package com.maimang.platform.crm.service.impl;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.maimang.platform.crm.config.DingTalk;
import com.maimang.platform.crm.service.DingTalkLoginService;
import com.maimang.platform.crm.util.AccessTokenUtil;
import org.springframework.stereotype.Service;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname DingTalkLoginServiceImpl
 * @description <br>钉钉登录service实现</br>
 * @date 2019-09-30 11:14
 * @email jintaozhao@qq.com
 */
@Service
public class DingTalkLoginServiceImpl implements DingTalkLoginService {

	/**
	 * 获取应用后台免登的accessToken
	 *
	 * @param corpid     企业Id
	 * @param corpsecret 专属的SSOSecret
	 * @return
	 * @throws Exception
	 */
	@Override
	public String getToken(String corpid, String corpsecret) throws Exception {
		DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/sso/gettoken");
		OapiSsoGettokenRequest request = new OapiSsoGettokenRequest();
		request.setCorpid(corpid);
		request.setCorpsecret(corpsecret);
		request.setHttpMethod("GET");
		OapiSsoGettokenResponse response = client.execute(request);
		if (response.getErrcode() == 0L)
			return response.getAccessToken();
		else
			return response.getErrmsg();
	}

	/**
	 * 获取应用管理员的身份信息
	 *
	 * @param accessToken 此token不同于一般的accessToken，需要调用获取微应用管理员免登需要的AccessToken
	 * @param code        通过Oauth认证给URL带上的CODE
	 * @return
	 * @throws Exception
	 */
	@Override
	public OapiSsoGetuserinfoResponse getUserInfo(String accessToken, String code) throws Exception {
		DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/sso/getuserinfo");
		OapiSsoGetuserinfoRequest request = new OapiSsoGetuserinfoRequest();
		request.setCode(code);
		request.setAccessToken(accessToken);
		request.setHttpMethod("GET");
		OapiSsoGetuserinfoResponse response = client.execute(request, accessToken);
		return response;
	}

	/**
	 * 获取应用管理员的身份信息
	 *
	 * @param code 通过Oauth认证给URL带上的CODE
	 * @return
	 * @throws Exception
	 */
	@Override
	public OapiSsoGetuserinfoResponse getUserInfo(String code) throws Exception {
		String accessToken = this.getToken(DingTalk.CorpId, DingTalk.SSOsecret);
		return this.getUserInfo(accessToken, code);
	}

	/**
	 * 获取用户userid
	 *
	 * @param code 免登授权码
	 * @return
	 * @throws Exception
	 */
	@Override
	public String getUserid(String code) throws Exception {
		DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
		OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();
		request.setCode(code);
		request.setHttpMethod("GET");
		String accessToken = AccessTokenUtil.getToken();
		OapiUserGetuserinfoResponse response = client.execute(request, accessToken);
		String userId = response.getUserid();
		return userId;
	}

	/**
	 * @param code 用户授权的临时授权码code，只能使用一次
	 * @return
	 * @throws Exception
	 */
	@Override
	public OapiSnsGetuserinfoBycodeResponse.UserInfo getUserInfoByCode(String code) throws Exception {
		DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
		OapiSnsGetuserinfoBycodeRequest req = new OapiSnsGetuserinfoBycodeRequest();
		req.setTmpAuthCode(code);
		OapiSnsGetuserinfoBycodeResponse response = client.execute(req, DingTalk.appId, DingTalk.appSecret);

		if (response.getErrcode() == 0L) {
			return response.getUserInfo();
		}
		return null;
	}

	@Override
	public String getAuthToken(String appid, String appSerect) throws Exception {
		DefaultDingTalkClient client = new DefaultDingTalkClient("");
		OapiSnsGettokenRequest request = new OapiSnsGettokenRequest();
		request.setAppid(appid);
		request.setAppsecret(appSerect);
		request.setHttpMethod("GET");
		OapiSnsGettokenResponse response = client.execute(request);
		return response.getAccessToken();
	}

	/**
	 * 根据token获取永久授权码
	 *
	 * @param accessToken TOKEN
	 * @return
	 * @throws Exception
	 */
	@Override
	public String getPersistentCode(String accessToken) throws Exception {
		return null;
	}

	/**
	 * 获取用户授权的SNS_TOKEN
	 *
	 * @param accessToken
	 * @param openId
	 * @param persistentCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public String getSnsToken(String accessToken, String openId, String persistentCode) throws Exception {
		return null;
	}
}
